package com.quectel.core.module.help.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.help.dao.FeedbackDao;
import com.quectel.core.module.help.dto.FeedbackDto;
import com.quectel.core.module.help.entity.FeedbackEntity;
import com.quectel.core.module.help.service.FeedbackService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/04/24 18:38
 */
@DubboService
public class FeedbackServiceImpl implements FeedbackService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FeedbackServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> FEEDBACK_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "FEEDBACK_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<FeedbackDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(FEEDBACK_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private FeedbackDao feedbackDao;
    @Autowired
    private VillageService villageService;


    @Override
    public FeedbackDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                FEEDBACK_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public FeedbackDto selectById(Long id) {
        FeedbackEntity entity = feedbackDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, FeedbackDto.class));
    }

    @Override
    public List<FeedbackDto> queryList(Map<String, Object> params) {
        List<FeedbackEntity> list = feedbackDao.queryList(params);
        List<FeedbackDto> result = CopyUtils.copyList(list, FeedbackDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private FeedbackDto paddingField(FeedbackDto dto) {
        if (dto != null) {
            if (dto.getVillageId() != null) {
                VillageDto villageDto = villageService.selectCacheById(dto.getVillageId());
                if (villageDto != null) {
                    dto.setVillageName(villageDto.getName());
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return feedbackDao.queryTotal(params);
    }

    @Override
    public Long save(FeedbackDto dto) {
        FeedbackEntity entity = CopyUtils.copyObj(dto, FeedbackEntity.class);
        entity.setId(Snowflake.nextId());
        feedbackDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(FeedbackDto dto) {
        FeedbackEntity entity = CopyUtils.copyObj(dto, FeedbackEntity.class);

        feedbackDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(FeedbackDto dto) {
        FeedbackEntity entity = CopyUtils.copyObj(dto, FeedbackEntity.class);

        MyBatisPlusUtils.updateAllColumnById(entity, feedbackDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        feedbackDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
